package com.dcy.singleton.type3;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/1/19 14:30
 */
public class SingletonTest03 {
    public static void main(String[] args) {
        // 测试
        System.out.println("懒汉式(线程不安全)");
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
    }
}

/**
 * 懒汉式(线程不安全)
 */
class Singleton {
    private static Singleton singleton;

    private Singleton() {
    }

    // 通过一个静态的公有方法，当使用到该方法时，才去创建instance
    // 即懒汉式
    public static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
}